package top.asteam.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.asteam.exception.BusinessException;
import top.asteam.entity.CommonResult;

/**
 * @Author Rock Wang
 * @Time 2021/4/11
 */
@Slf4j
@RestControllerAdvice("top/asteam/springcloud")//限制拦截的包范围
public class ControllerExceptionAdvice {

    @ExceptionHandler(value = BusinessException.class)//拦截BusinessException异常
    @ResponseBody//返回对象给前端
    public Object exceptionHandler(BusinessException e){
        log.error("",e);
        return CommonResult.serverError(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)//拦截BusinessException异常
    @ResponseBody//返回对象给前端
    public Object exceptionHandler(Exception e){
        log.error("",e);
        return CommonResult.serverError("调用异常");
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)//拦截指定异常
    @ResponseBody//返回对象给前端
    public Object exceptionHandler(MethodArgumentNotValidException e){
        log.error("",e);
        return CommonResult.serverError("参数校验异常，请检查入参！");
    }

}
